#include <iostream>
#include <map>
#include <cstdio>

using namespace std;

typedef long long LL;

bool check(char ch)
{
    if(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )
        return true;
    else
        return false;
}

int main()
{
    int n,p;
    cin >> n >> p;
    LL ret = 0;
    map<string,int> mp;
    for(int i = 1;i <= n;i++)
    {
        string s;int m;
        cin >> s >> m;
        mp[s] = m;
    }
    char ch;
    string t = "";
    while(scanf("%c",&ch) != EOF)
    {
        if(check(ch)) t += ch;
        else
        {
            ret = (ret + mp[t]) % p ;
            t = "";
        }
    }
    cout << ret;

}